package m.jclecai.com.fragment;

import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;

import com.squareup.okhttp.Request;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.OnClick;
import m.jclecai.com.R;
import m.jclecai.com.adapter.PayRedpacketAdapter;
import m.jclecai.com.base.BaseFragment;
import m.jclecai.com.bean.RedPacketBean;
import m.jclecai.com.constant.Constant_Url;
import m.jclecai.com.listener.NotifyTextViewSetChanged;
import m.jclecai.com.utils.MyLog;
import m.jclecai.com.utils.MyToast;
import m.jclecai.com.utils.PreferenceUtil;
import m.jclecai.com.utils.http.FastOk;
import m.jclecai.com.utils.http.HttpManager;
import m.jclecai.com.utils.http.responseBody.BaseBody;
import m.jclecai.com.utils.http.responseBody.RedpacketBody;

/**
 * Created by ZD on 2017/6/27.
 */

public class BuyRedPkgFragment extends BaseFragment implements NotifyTextViewSetChanged {

    @BindView(R.id.rv_pkg)
    RecyclerView rv_pkg;
    PayRedpacketAdapter adapter;
    LinearLayoutManager manager;
    List<RedPacketBean> redPcks;
    RedPacketBean redPacketBean;

    @Override
    public int getLayoutId() {
        return R.layout.fragment_buy_red;
    }

    @Override
    public void initData() {
        super.initData();
        redPcks = new ArrayList<>();
        //设置布局管理器
        manager = new GridLayoutManager(getActivity(),3);
        manager.setOrientation(LinearLayoutManager.VERTICAL);
        rv_pkg.setLayoutManager(manager);
        adapter = new PayRedpacketAdapter(getActivity(), redPcks, this);
        adapter.setFrom(PayRedpacketAdapter.FROM.BuyRedPkgFragment);
        rv_pkg.setAdapter(adapter);
        showLoadingLayout(rv_pkg);
        getRedPacket();
    }

    @Override
    public void onClickSetChangedText() {
        adapter.notifyDataSetChanged();
        if (adapter.getRedCheckIndex() > -1) {
            redPacketBean = redPcks.get(adapter.getRedCheckIndex());
        }
    }

    @OnClick(R.id.tv_buy)
    void buyRedPkg(){
        if(redPacketBean == null){
            MyToast.showToast(getActivity(),"请选择需要购买的红包");
            return;
        }
        showDialog();
        Map<String,String> map = new HashMap<>();
        map.put("user", PreferenceUtil.getUserName(getActivity()));
        map.put("password", PreferenceUtil.getPassword(getActivity()));
        map.put("id", redPacketBean.getId());
        map.put("money", String.valueOf(redPacketBean.getAllmoney()));
        map.put("newValue", redPacketBean.getBuymoney());
        HttpManager.postAsyn(Constant_Url.MY_REDPACKET_BUY, new HttpManager.StringCallback() {
            @Override
            public void onFailure(Request request, IOException e) {
                dissDialog();
                MyLog.i(request.toString());
                MyToast.showToast(getActivity(),getString(R.string.error_network));
            }

            @Override
            public void onResponse(String response) {
                dissDialog();
                BaseBody body = FastOk.get(response, BaseBody.class);
                if(body != null){
                    MyToast.showToast(getActivity(),body.getMessage());
                }else {
                    MyToast.showToast(getActivity(),getString(R.string.error_network));
                }
            }
        },map);
    }

    /**
     * 是否直接展开红包
     */
    void getRedPacket() {
        HttpManager.getAsyn(Constant_Url.REDPACKET_LIST, new HttpManager.StringCallback() {
            @Override
            public void onFailure(Request request, IOException e) {
                MyLog.i(request.toString());
                showInternetOffLayout();
            }

            @Override
            public void onResponse(String response) {
                RedpacketBody body = FastOk.get(response, RedpacketBody.class);
                closeLoadingLayout();
                if (body != null && body.getStatus() == 0) {
                    redPcks.clear();
                    redPcks.addAll(body.getRecords());
                    adapter.notifyDataSetChanged();
                }
                if(redPcks.size() <= 0)
                    showEmptyLayout();
            }
        });

    }

    @Override
    public void OnRetry(String btnMessage) {
        super.OnRetry(btnMessage);
        showLoadingLayout(rv_pkg);
        getRedPacket();
    }
}
